package com.eqdd.yiqidian.ui.gongneng.renshi.yuangongdangan.yuangongzhuangkuang;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ZaizhiRenyuanListActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ListModelResultBean;
import com.eqdd.yiqidian.bean.rebuild.ZaizhiRenyuanListResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import org.reactivestreams.Publisher;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class LizhiRenyuanListActivity extends BaseActivity {
    private ZaizhiRenyuanListActivityCustom dataBinding;
    private BaseQuickAdapter<String, BaseViewHolder> quickAdapter;
    private List<String> strings;
    private SlimAdapterEx slimAdapterEx;
    private ModelRx2DataSource<ZaizhiRenyuanListResultBean> dataSource;
    private MVCSwipeRefreshHelper<List<ZaizhiRenyuanListResultBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_zaizhi_renyuan_list);
        initTopTitleBar(View.VISIBLE, "离职人员");
    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_10, new SlimInjector<ZaizhiRenyuanListResultBean>() {

            @Override
            public void onInject(ZaizhiRenyuanListResultBean data, IViewInjector injector) {

                injector.with(R.id.iv_head, v -> ImageUtil.setCircleImage(data.getUiphoto(), (ImageView) v))
                        .gone(R.id.iv_enter)
                        .text(R.id.tv_upper_left_content, data.getUname())
                        .text(R.id.tv_upper_right_content, data.getUserPhone())
                        .text(R.id.tv_down_left_content, data.getDepartName() + "-" + data.getPostName())
                        .text(R.id.tv_down_right_content, data.getSignEntryTime());
            }
        }).attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Config.ZAIZHIYUANGONG_INFO, (Serializable) slimAdapterEx.getData().get(position));
                    startActivity(YuangongDocumentActivity.class, bundle);
                });
        slimAdapterEx.updateData(new ArrayList<ZaizhiRenyuanListResultBean>());
        dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResult<ListModelResultBean>>post(HttpConfig.BASE_URL + HttpConfig.LIZHI_YUANGONG_LIST)
                        .params("companyId", user.getCompanyId())
                        .params("page", pageNum)
                        .converter(new JsonConverter<HttpResult<ListModelResultBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        })
                        .adapt(new FlowableBody<HttpResult<ListModelResultBean>>())
                        .flatMap(new Function<HttpResult<ListModelResultBean>, Publisher<List<ZaizhiRenyuanListResultBean>>>() {
                            @Override
                            public Publisher<List<ZaizhiRenyuanListResultBean>> apply(@NonNull HttpResult<ListModelResultBean> listHttpResult) throws Exception {
                                pageNum = listHttpResult.getItems().getPage();
                                return Flowable.just(listHttpResult.getItems().getListModel() == null ? new ArrayList<ZaizhiRenyuanListResultBean>() : listHttpResult.getItems().getListModel());
                            }
                        });

            }
        });
        mvcHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }

    @Override
    public void setView() {
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
